package com.ctsi.organise.app.rest;

import com.ctsi.commons.util.UtilMisc;
import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.request.UtilHttp;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.common.response.UnifiedResponse;
import com.ctsi.framework.mybatis.support.VoTemplate;
import com.ctsi.framework.organise.security.UserInfo;
import com.ctsi.framework.security.util.SecurityUtil;
import com.ctsi.organise.entity.PersonRY;
import com.ctsi.organise.query.PersonRyQuery;
import com.ctsi.organise.service.PersonRyService;
import com.ctsi.organise.vo.PersonRyVO;
import com.ctsi.product.entity.Upload;
import com.ctsi.product.service.UploadService;
import com.ctsi.system.service.api.DictionaryServiceProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @auth:wgf
 * @description :
 * @Date: 2019/3/20
 */
@RestController
public class PersonRYController {
    @Autowired
    private PersonRyService personRyService;
    @Autowired
    private VoTemplate voTemplate;
    @Autowired
    private UploadService uploadService;
    @Autowired
    private DictionaryServiceProxy dictionaryServiceProxy;
    @Value("${file-store.host}")
    private String fileServerHost;


    /**
     * 荣誉列表
     */
    @PostMapping(value = "/person/queryListPersonRY",consumes = "application/json",produces = "application/json;charset=UTF-8")
    @ResponseStatus(HttpStatus.OK)
    public UnifiedResponse<Page<PersonRyVO>> queryListPersonRY(@RequestBody PersonRyQuery ppp){
        Page<PersonRY> result = personRyService.queryListPersonRY(UtilHttp.filterBlank(UtilMisc.toMap(ppp)),ppp.getStart(),ppp.getPageSize());
        List<PersonRyVO> personRyVOList = result.getResult().stream().map(lib -> {
            PersonRyVO personRyVO = voTemplate.defaultExcute(new PersonRyVO(), lib);
            String ry_typename = dictionaryServiceProxy.getDictValue("ry_type", lib.getRy_type());
            String rydj=dictionaryServiceProxy.getDictValue("rydj_type",lib.getRydj());
            String webPath = lib.getImgurl().replace("\\", "/");
            String url = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/")) +
                    (webPath.startsWith("/") ? "files/org/api/file" : "files/org/api/file/") + webPath;
            personRyVO.setImgurl(url);
            personRyVO.setRy_dj_type(rydj);
            personRyVO.setRy_type_name(ry_typename);
            return personRyVO;
        }).collect(Collectors.toList());
        return UnifiedResponse.ok(new Page<>(ppp.getStart(),result.getTotalCount(),result.getPageSize(),personRyVOList));
    }


    /**
     * 添加及修改接口
     */
    @PostMapping(value = "/person/addOrEditRY",consumes = "application/json",produces = "application/json;charset=UTF-8")
    @ResponseStatus(HttpStatus.OK)
    public UnifiedResponse<Integer> addOrEditRY(@RequestBody PersonRY personRY){
        UserInfo user = SecurityUtil.getUser();
        if(UtilValidate.isEmpty(personRY.getId())){
            String id= String.valueOf(UUIDGenerator.getId());
            personRY.setId(id);
            personRY.setCreatetime(System.currentTimeMillis());
            if(UtilValidate.isNotEmpty(personRY.getImgurl())){
                Upload upload=new Upload();
                upload.setUserid(user.getUserId());
                upload.setCreatetime(System.currentTimeMillis());
                upload.setFilename(personRY.getImgname());
                upload.setIsdefault("0");
                upload.setPersonryid(personRY.getId());
                upload.setSavepath(personRY.getImgurl());
                uploadService.insertUpload(upload);
            }
            int i = personRyService.insertPersonRY(personRY);
            return UnifiedResponse.ok(i);
        }else {
            String imgurl="";
            String name="";
            String name1="";
            if(UtilValidate.isNotEmpty(personRY.getImgurl())){
            String url=personRY.getImgurl();
                String temp[] = url.replaceAll("\\\\","/").split("/");
                if (temp.length > 1) {
                    name = temp[temp.length - 1];
                    name1= temp[temp.length - 2];
                }
            if(url.startsWith("http")==true){
                imgurl= name1+ File.separator+name;
            }else {
                imgurl=url;
            }
                Upload upload=new Upload();
                upload.setUserid(user.getUserId());
                upload.setCreatetime(System.currentTimeMillis());
                upload.setFilename(personRY.getImgname());
                upload.setIsdefault("0");
                upload.setPersonryid(personRY.getId());
                upload.setSavepath(imgurl);
                uploadService.insertUpload(upload);
            }
            personRY.setImgurl(imgurl);
            int i = personRyService.updatePersonRyByPKSelective(personRY);
            return UnifiedResponse.ok(i);
        }
    }
    /**
     * 查询荣誉详情
     */
    @PostMapping(value = "/person/findPersonRYById",produces = "application/json;charset=UTF-8" )
    @ResponseStatus(HttpStatus.OK)
    public UnifiedResponse<PersonRY> findPersonRYById(@RequestParam("id") String id){
        if (UtilValidate.isEmpty(id)){
            return UnifiedResponse.ok(null);
        }
        PersonRY result = personRyService.findPersonRyById(id);
        String webPath = result.getImgurl().replace("\\", "/");
        String url = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/")) +
                (webPath.startsWith("/") ? "files/org/api/file" : "files/org/api/file/") + webPath;
        result.setImgurl(url);
        return UnifiedResponse.ok(result);
    }
    /**
     * 删除荣誉
     */
    @PostMapping(value = "/person/deletePersonRy",produces = "application/json;charset=UTF-8")
    @ResponseStatus(HttpStatus.OK)
    public UnifiedResponse<Integer> deletePersonRy(@RequestParam("id") String id){
        if (UtilValidate.isNotEmpty(id)){
            PersonRY personRy=new PersonRY();
            personRy.setIs_delete(1);
            personRy.setId(id);
            int i = personRyService.updatePersonRyByPKSelective(personRy);
            return UnifiedResponse.ok(i);
        }else {
            return UnifiedResponse.ok(0);
        }
    }

}
